package com.lanou.horo.zhuizhui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.horo.zhuizhui.entity.RecentComicsItem;
import com.lanou.horo.zhuizhui.R;
import com.lanou.horo.zhuizhui.utility.imageLoader.AsyncImageLoader;
import com.lanou.horo.zhuizhui.utility.customImage.RoundAngleImageView;
import com.lanou.horo.zhuizhui.utility.imageLoader.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/11/2.
 */
public class RecentComicsAdapter extends
        RecyclerView.Adapter<RecentComicsAdapter.RecentComicsViewHolder> {
    private List<RecentComicsItem.ComicsEntity> entities;
    private RecentComicsItem.ThemeEntity themeEntity;
//    private AsyncImageLoader imageLoader;
    private Context context;
private ImageLoader imageLoader;

    public RecentComicsAdapter(Context context) {
        this.context = context;
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    public void addTheme(RecentComicsItem.ThemeEntity th) {
        themeEntity = th;
        notifyDataSetChanged();
    }

    public void addComicsList(List<RecentComicsItem.ComicsEntity> datas) {
        entities = datas;
        notifyDataSetChanged();
    }

    @Override
    public RecentComicsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.activity_comics_recent_item, null);
        return new RecentComicsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecentComicsViewHolder holder, int position) {

        if (entities != null && entities.size() > 0) {
            RecentComicsItem.ComicsEntity entity = entities.get(position);
            holder.recentNameTV.setText(entity.getName());
            holder.recentAuthorTV.setText("作者:" + entity.getAuthor());
            holder.recentCategoryNameTV.setText("类型:" + entity.getCategory_name());
            holder.recentUpdateTV.setText("更新:" + entity.getLast_volume_updated_at());
            holder.recentLastVolumeTV.setText("更新至:" + entity.getLast_volume());
            String url = entity.getCover_img();
            holder.recentIV.setImageUrl(url,imageLoader);
//            setBitmap(url, holder.recentIV);
        }
        if (themeEntity != null) {
            if (themeEntity.getName().equals("排行榜")) {
                holder.rankingTv.setText("" + (position + 1));
            }
        }
    }

    @Override
    public int getItemCount() {
        return entities != null && entities.size() > 0 ? entities.size() : 0;
    }

    public class RecentComicsViewHolder extends RecyclerView.ViewHolder {
        private TextView recentNameTV, recentAuthorTV, recentCategoryNameTV,
                recentUpdateTV, recentLastVolumeTV, rankingTv;
        private RoundAngleImageView recentIV;

        public RecentComicsViewHolder(View itemView) {
            super(itemView);
            recentIV = (RoundAngleImageView) itemView.findViewById(R.id.recentImageView);
            recentNameTV = (TextView) itemView.findViewById(R.id.recentNameTextView);
            recentAuthorTV = (TextView) itemView.findViewById(R.id.recentAuthorTextView);
            recentCategoryNameTV = (TextView) itemView.findViewById(R.id.recentCategoryNameTextView);
            recentUpdateTV = (TextView) itemView.findViewById(R.id.recentUpdateTextView);
            recentLastVolumeTV = (TextView) itemView.findViewById(R.id.recentLastVolumeTextView);
            rankingTv = (TextView) itemView.findViewById(R.id.rankingTextview);
        }
    }

//    private void setBitmap(String url, ImageView imageView) {
//        // 给 ImageView 设置一个 tag
//        imageView.setTag(url);
//        // 预设一个图片
//        imageView.setImageResource(R.mipmap.loading);
//        if (!TextUtils.isEmpty(url)) {
//            Bitmap bitmap = imageLoader.loadImage(imageView, url);
//            if (bitmap != null) {
//                imageView.setImageBitmap(bitmap);
//            }
//        }
//    }
}
